package com.dayou.service.impl;

import java.util.List;

import com.dayou.bean.MqMessageBean;
import com.dayou.bean.SysDept;
import com.dayou.common.exception.BusinessException;
import com.dayou.common.utils.StringUtils;
import com.dayou.service.MqService;
import com.dayou.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.dayou.mapper.BringRecordMapper;
import com.dayou.bean.BringRecord;
import com.dayou.service.IBringRecordService;
import com.dayou.common.core.text.Convert;
import com.dayou.bean.SysUser;
import org.springframework.transaction.annotation.Transactional;

/**
 * 提审会见值班室民警带人记录 服务层实现
 * 
 * @author dayou
 * @date 2020-05-11
 */
@Service
public class BringRecordService implements IBringRecordService
{
	@Autowired
	private BringRecordMapper bringRecordMapper;
	@Autowired
	private SysDeptService deptService;
	@Autowired
	private MqService mqService;
	@Value("${interview.module-name}")
	private String moduleName;

	/**
     * 查询提审会见值班室民警带人记录信息
     * 
     * @param id 提审会见值班室民警带人记录ID
     * @return 提审会见值班室民警带人记录信息
     */
    @Override
	public BringRecord selectBringRecordById(Integer id)
	{
	    return bringRecordMapper.selectBringRecordById(id);
	}
	
	/**
     * 查询提审会见值班室民警带人记录列表
     * 
     * @param bringRecord 提审会见值班室民警带人记录信息
     * @return 提审会见值班室民警带人记录集合
     */
	@Override
	public List<BringRecord> selectBringRecordList(BringRecord bringRecord)
	{
		SysUser loginUser = UserUtil.getUser();
		Integer deptId = loginUser.getDeptId();
		SysDept dept = deptService.selectSysDeptById(deptId);
		if("值班室".equals(dept.getName())){
			Integer floor = Integer.parseInt(loginUser.getNickname().substring(0,1));
			return bringRecordMapper.selectBringRecordListByFloor(floor,null,null);
		}else{
			return bringRecordMapper.selectBringRecordList(bringRecord);
		}
	}

	/**
     * 新增提审会见值班室民警带人记录
     * 
     * @param bringRecord 提审会见值班室民警带人记录信息
     * @return 结果
     */
	@Override
	@Transactional
	public int insertBringRecord(BringRecord bringRecord)
	{
		SysUser loginUser = UserUtil.getUser();
		if(loginUser != null){
			bringRecord.setCreateBy(loginUser.getId());
		}
	    int resu = bringRecordMapper.insertBringRecord(bringRecord);
	    if(resu > 0){
			mqService.sendMessage(new MqMessageBean("duty", "new-record", "", moduleName));
			mqService.sendMessage(new MqMessageBean("comparison", "new-record", "", moduleName));
			return resu;
	    }else {
	    	throw new BusinessException("新增失败");
		}
	}
	
	/**
     * 修改提审会见值班室民警带人记录
     * 
     * @param bringRecord 提审会见值班室民警带人记录信息
     * @return 结果
     */
	@Override
	public int updateBringRecord(BringRecord bringRecord)
	{
		SysUser loginUser = UserUtil.getUser();
		bringRecord.setUpdateBy(loginUser == null ? 1 : loginUser.getId());
		mqService.sendMessage(new MqMessageBean("duty", "refresh-record", "", moduleName));
		mqService.sendMessage(new MqMessageBean("comparison", "refresh-record", "", moduleName));
		return bringRecordMapper.updateBringRecord(bringRecord);
	}

	/**
     * 删除提审会见值班室民警带人记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteBringRecordByIds(String ids)
	{
	    if(StringUtils.isEmpty(ids)){
	        throw new BusinessException("未获取到id");
		}else{
	        String[] idsArr = Convert.toStrArray(ids);
	        if(idsArr.length == 1){
                try{
                    Integer id = Integer.parseInt(idsArr[0]);
                    return bringRecordMapper.deleteBringRecordById(id);
				}catch (Exception ex){
	                throw new BusinessException("id有误");
				}
			}else{
                return bringRecordMapper.deleteBringRecordByIds(idsArr);
            }
		}
	}
}
